package ruifeng.zcj.dig.groble.addphoto;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.photopicker.activity.BGAPPToolbarActivity;
import cn.bingoogolapple.photopicker.activity.BGAPhotoPickerActivity;
import cn.bingoogolapple.photopicker.activity.BGAPhotoPickerPreviewActivity;
import cn.bingoogolapple.photopicker.widget.BGASortableNinePhotoLayout;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;
import ruifeng.zcj.dig.R;
import ruifeng.zcj.dig.groble.FragmentArgs;

/**
 * Created by Administrator on 2016/10/18 0018.
 */
public class AddPhotoActivity extends BGAPPToolbarActivity implements EasyPermissions.PermissionCallbacks, BGASortableNinePhotoLayout.Delegate {
    private static final int REQUEST_CODE_PERMISSION_PHOTO_PICKER = 1;

    private static final int REQUEST_CODE_CHOOSE_PHOTO = 1;
    private static final int REQUEST_CODE_PHOTO_PREVIEW = 2;

    private static final String EXTRA_MOMENT = "EXTRA_MOMENT";
    private static final String EXTRA_PHOTOS = "PHOTOS";

    private static final int MAX_PHOTO_COUNT = 9;


    private Button mFirum;
    private BGASortableNinePhotoLayout mPhotosSnpl;

    public static ArrayList<String> getPhotoArrayList(Intent intent){
        Bundle photos =  intent.getExtras();
        FragmentArgs args = (FragmentArgs) photos.getSerializable(EXTRA_MOMENT);
        return (ArrayList<String>) args.get(EXTRA_PHOTOS);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_add_photo);

        mFirum = getViewById(R.id.email_sign_in_buttons);
        mPhotosSnpl = getViewById(R.id.snpl_moment_add_photos);
    }

    @Override
    protected void setListener() {
        // 设置拖拽排序控件的代理
        mPhotosSnpl.setIsSortable(true);
        mPhotosSnpl.setDelegate(this);
        mFirum.setOnClickListener(this);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        setTitle("添加图片");
        mPhotosSnpl.init(this);
    }

    public void onClick(View v) {
        if (v.getId() == R.id.email_sign_in_buttons) {
            Intent intent = new Intent();
            FragmentArgs fragmentArgs2 = new FragmentArgs();
            fragmentArgs2.add(EXTRA_PHOTOS, mPhotosSnpl.getData());

            intent.putExtra(EXTRA_MOMENT, fragmentArgs2);
            setResult(RESULT_OK, intent);
            finish();
        }
    }

    @Override
    public void onClickAddNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, ArrayList<String> models) {
        choicePhotoWrapper();
    }

    @Override
    public void onClickDeleteNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {
        mPhotosSnpl.removeItem(position);
    }

    @Override
    public void onClickNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {
        startActivityForResult(BGAPhotoPickerPreviewActivity.newIntent(this, MAX_PHOTO_COUNT, models, models, position, false), REQUEST_CODE_PHOTO_PREVIEW);
    }

    @AfterPermissionGranted(REQUEST_CODE_PERMISSION_PHOTO_PICKER)
    private void choicePhotoWrapper() {
        String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if (EasyPermissions.hasPermissions(this, perms)) {
            // 拍照后照片的存放目录，改成你自己拍照后要存放照片的目录。如果不传递该参数的话就没有拍照功能
            File takePhotoDir = new File(Environment.getExternalStorageDirectory(), "zcj");

            startActivityForResult(BGAPhotoPickerActivity.newIntent(this, takePhotoDir, MAX_PHOTO_COUNT, mPhotosSnpl.getData()), REQUEST_CODE_CHOOSE_PHOTO);
        } else {
            EasyPermissions.requestPermissions(this, "图片选择需要以下权限:\n\n1.访问设备上的照片", REQUEST_CODE_PERMISSION_PHOTO_PICKER, perms);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        if (requestCode == REQUEST_CODE_PERMISSION_PHOTO_PICKER) {
            Toast.makeText(this, "您拒绝了「图片选择」所需要的相关权限!", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_CHOOSE_PHOTO) {
            mPhotosSnpl.setData(BGAPhotoPickerActivity.getSelectedImages(data));
        } else if (requestCode == REQUEST_CODE_PHOTO_PREVIEW) {
            mPhotosSnpl.setData(BGAPhotoPickerPreviewActivity.getSelectedImages(data));
        }
    }
}
